package com.zyf;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelTest {

    String path= "C:\\Users\\zyf\\Desktop/";

    @Test
    public void testExcel03() throws Exception {
        //1、创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //2、创建一个工作表
        Sheet sheet = workbook.createSheet("张忆菲记录表");
        //3、创建一个行 (1,1)
        Row row1 = sheet.createRow(0);
        //4、创建一个单元格（1，1）
        Cell cell11 = row1.createCell(0);
        //给单元格里写入内容（1，1）
        cell11.setCellValue("今天学习内容");
        //创建第二个单元格(1,2)
        Cell cell12 = row1.createCell(1);
        //给单元格里写入内容（1，2）
        cell12.setCellValue("POI");



        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //（2，2）
        Cell cell22 = row2.createCell(1);
        String time=new DateTime().toString("yyyy-MM-dd");
        cell22.setCellValue(time);

        //生成一张表（io流） 03版本就是用xls结尾！
        FileOutputStream fileOutputStream = new FileOutputStream(path+"张忆菲记录表03.xls");

        //写出
        workbook.write(fileOutputStream);

        //关闭io流
        fileOutputStream.close();

        System.out.println("excel生成完毕");
    }

    @Test
    public void testExcel07() throws Exception {
        //1、创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2、创建一个工作表
        Sheet sheet = workbook.createSheet("张忆菲记录表");
        //3、创建一个行 (1,1)
        Row row1 = sheet.createRow(0);
        //4、创建一个单元格（1，1）
        Cell cell11 = row1.createCell(0);
        //给单元格里写入内容（1，1）
        cell11.setCellValue("今天学习内容");
        //创建第二个单元格(1,2)
        Cell cell12 = row1.createCell(1);
        //给单元格里写入内容（1，2）
        cell12.setCellValue("POI");



        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //（2，2）
        Cell cell22 = row2.createCell(1);
        String time=new DateTime().toString("yyyy-MM-dd");
        cell22.setCellValue(time);

        //生成一张表（io流） 07版本就是用xlsx结尾！
        FileOutputStream fileOutputStream = new FileOutputStream(path+"张忆菲记录表07.xlsx");

        //写出
        workbook.write(fileOutputStream);

        //关闭io流
        fileOutputStream.close();

        System.out.println("excel生成完毕");
    }

    @Test
    public void testExcel03Time() throws IOException {
        //开始时间
        Long begin = System.currentTimeMillis();

        //创建一个薄
        Workbook workbook = new HSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int i = 0; i <65536 ; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);
            }
        }
        FileOutputStream outputStream = new FileOutputStream(path+"testExcel03Time.xls");
        workbook.write(outputStream);
        outputStream.close();
        //结束时间
        Long end = System.currentTimeMillis();

        //计算时间
        System.out.println((double) (end-begin)/1000);
    }


    @Test
    public void testExcel07Time() throws IOException {
        //开始时间
        Long begin = System.currentTimeMillis();

        //创建一个薄
        Workbook workbook = new XSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int i = 0; i <70000 ; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);
            }
        }
        FileOutputStream outputStream = new FileOutputStream(path+"testExcel07Time.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        //结束时间
        Long end = System.currentTimeMillis();

        //计算时间
        System.out.println((double) (end-begin)/1000);
    }


    @Test
    public void testExcel07TimeS() throws IOException {
        //开始时间
        Long begin = System.currentTimeMillis();

        //创建一个薄
        Workbook workbook = new SXSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int i = 0; i <80000 ; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);
            }
        }
        FileOutputStream outputStream = new FileOutputStream(path+"testExcel07TimeS.xlsx");
        workbook.write(outputStream);
        //清除临时文件！
        ((SXSSFWorkbook) workbook).dispose();
        outputStream.close();
        //结束时间
        Long end = System.currentTimeMillis();

        //计算时间
        System.out.println((double) (end-begin)/1000);
    }

}
